package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;

import com.turing.post.cardmanagement.util.ActivateCard;
import com.turing.post.cardmanagement.util.ActivateCardBusiness;
import com.turing.post.cardmanagement.util.ActivateCardDao;
import com.turing.post.cardmanagement.util.CardEventLog;
import com.turing.post.cardmanagement.util.CardEventLogDao;
import com.turing.post.cardmanagement.util.CardLoss;
import com.turing.post.cardmanagement.util.CardLossDao;
import com.turing.post.cardmanagement.util.CardManaApplicationException;
import com.turing.post.cardmanagement.util.MakeCardControl;
import com.turing.post.cardmanagement.util.MakeCardControlDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.SysBranchDao;
import com.turing.post.salesmanage.util.PersonalCustom;
import com.turing.post.salesmanage.util.PersonalCustomDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "cardmanagement.impl.ActivateCardBusinessImpl")
@Remote
@Local
public class ActivateCardBusinessImpl implements ActivateCardBusiness {

	@EJB(mappedName = "organization.impl.SysBranchDaoImpl")
	private SysBranchDao sysBranchDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;
	@EJB(mappedName = "cardmanagement.impl.ActivateCardDaoImpl")
	private ActivateCardDao acDao;

	@EJB(mappedName = "cardmanagement.impl.CardEventLogDaoImpl")
	private CardEventLogDao celDao;

	@EJB(mappedName = "salesmanage.impl.PersonalCustomDaoImpl")
	private PersonalCustomDao pcDao;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.CardLossDaoImpl")
	private CardLossDao clDao;

	@EJB(mappedName = "cardmanagement.impl.MakeCardControlDaoImpl")
	private MakeCardControlDao mccDao;

	/**
	 * 生成卡事件单号
	 * 
	 * @param operId
	 * @return
	 * @throws PCPSApplicationException
	 */
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	private String createCardEventSeqNo(String operBranchNo, IssueCompany ic)
			throws PCPSApplicationException {
		// 单号
		Integer seqNo = sysBranchDaoImpl
				.updateSysBranchOrderSeqNo(operBranchNo);
		String cardEventOrderSeq = operBranchNo
				+ PCPSUtil.getPackedYMDString(ic.getTranDate())
				+ String.format("%05d", seqNo);
		return cardEventOrderSeq;
	}

	/**
	 * 解除卡密码锁定
	 */
	@Override
	public String resetPwdErrCnt(PCPSWebInput webInput, String cardNo)
			throws PCPSApplicationException {
		// 登录用户检测
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"登录操作员所属销售机构号为空!");
		}
		// 卡号为空
		if (PCPSUtil.isEmpty(cardNo)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"检索失败,卡号不能为空!");
		}
		// 卡是否存在
		ActivateCard ac = acDao.getActivateCardByCardNoForUpdate(cardNo);
		if (null == ac) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"检索失败,没有找到此卡信息!");
		}
		// 卡所属发卡商和操作员所属发卡商是否一致
		MakeCardControl mcc = mccDao.getMakeCardControlByBatchNo(ac
				.getBatchNo());
		if (mcc != null) {
			if (!mcc.getIssueCompanyNo().equals(
					PCPSUtil.getIssueCompanyNo(webInput))) {
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"卡所属发卡商和操作员所属发卡商不一致!");
			}
		}
		IssueCompany ic = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(PCPSUtil
						.getIssueCompanyNo(webInput));
		// 判断卡状态
		if (ac.getPwdErrCnt() < ic.getMaxPwdErrCnt()) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"此卡不需要解除卡密码锁定!");
		}
		ac.setPwdErrCnt(0);
		ac.setLastPwdErrTime(null);
		acDao.update(ac);

		// 卡事件日志
		CardEventLog cel = new CardEventLog();
		cel.setCardEventId(commonDaoImpl.getUniqueIntegerKey());
		cel.setCardNo(cardNo);
		cel.setCardClassicId(ac.getCardClassicId());
		cel.setExpired(ac.getExpired());
		cel.setIssueCompanyNo(ic.getIssueCompanyNo());
		cel.setTranDate(ic.getTranDate());
		cel.setTranTime(commonDaoImpl.getDatabaseSysTime());
		cel.setBranchNo(webInput.getOperBranchNo());
		cel.setMasterAccNo(ac.getMasterAccNo());
		if (ac.getOwnerId() != null) {
			PersonalCustom pcObj = pcDao.getPersonalCustomByPersonalId(ac
					.getOwnerId());
			if (pcObj != null) {
				cel.setCardHolder(pcObj.getName());
				cel.setCardHolderIdType(pcObj.getIdTypeId());
				cel.setCardHolderIdNo(pcObj.getIdNo());
			}
		}
		cel.setEventType(CardEventLog.CARDEVENTLOG_EVENTTYPE_PWDERRUNLOCK);
		cel.setCardEventOrderSeq(createCardEventSeqNo(webInput
				.getOperBranchNo(), ic));
		cel.setBalance(ac.getBalance());
		cel.setFee(new BigInteger("0"));
		cel.setOperId(webInput.getOperId());
		cel.setRemark("强行解除密码错误锁定,卡余额:" + ac.getBalance() + ",总手续费:0");
		celDao.insert(cel);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Edit, "解除卡密码锁定", "CardNo:"
						+ cel.getCardNo());
		return cel.getCardEventId().toString();
	}

	/**
	 * 卡手工锁定
	 */
	@Override
	public String cardManualLock(PCPSWebInput webInput, String cardNo,
			String remark) throws PCPSApplicationException {
		// 登录用户检测
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"登录操作员所属销售机构号为空!");
		}
		// 卡号为空
		if (PCPSUtil.isEmpty(cardNo)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"检索失败,卡号不能为空!");
		}
		// 卡是否存在
		ActivateCard ac = acDao.getActivateCardByCardNoForUpdate(cardNo);
		if (null == ac) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"检索失败,没有找到此卡信息!");
		}
		// 卡所属发卡商和操作员所属发卡商是否一致
		MakeCardControl mcc = mccDao.getMakeCardControlByBatchNo(ac
				.getBatchNo());
		if (mcc != null) {
			if (!mcc.getIssueCompanyNo().equals(
					PCPSUtil.getIssueCompanyNo(webInput))) {
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"卡所属发卡商和操作员所属发卡商不一致!");
			}
		}
		// 判断卡状态
		if (ac.getManualLockStatus().equals(
				ActivateCard.ACTIVATECARD_MANUALLOCKSTATUS_LOCKED)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"此卡已经锁定!");
		}
		ac
				.setManualLockStatus(ActivateCard.ACTIVATECARD_MANUALLOCKSTATUS_LOCKED);
		acDao.update(ac);

		// 卡事件日志
		CardEventLog cel = new CardEventLog();
		cel.setCardEventId(commonDaoImpl.getUniqueIntegerKey());
		cel.setCardNo(cardNo);
		cel.setCardClassicId(ac.getCardClassicId());
		cel.setExpired(ac.getExpired());
		IssueCompany ic = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(PCPSUtil
						.getIssueCompanyNo(webInput));
		cel.setIssueCompanyNo(ic.getIssueCompanyNo());
		cel.setTranDate(ic.getTranDate());
		cel.setTranTime(commonDaoImpl.getDatabaseSysTime());
		cel.setBranchNo(webInput.getOperBranchNo());
		cel.setMasterAccNo(ac.getMasterAccNo());
		if (ac.getOwnerId() != null) {
			PersonalCustom pcObj = pcDao.getPersonalCustomByPersonalId(ac
					.getOwnerId());
			if (pcObj != null) {
				cel.setCardHolder(pcObj.getName());
				cel.setCardHolderIdType(pcObj.getIdTypeId());
				cel.setCardHolderIdNo(pcObj.getIdNo());
			}
		}
		cel.setCardEventOrderSeq(createCardEventSeqNo(webInput
				.getOperBranchNo(), ic));
		cel.setEventType(CardEventLog.CARDEVENTLOG_EVENTTYPE_MANUAL_LOCK);
		cel.setBalance(ac.getBalance());
		cel.setFee(new BigInteger("0"));
		cel.setOperId(webInput.getOperId());
		cel.setRemark(remark);
		celDao.insert(cel);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Edit, "卡手工锁定", "CardNo:" + cel.getCardNo());
		return cel.getCardEventId().toString();
	}

	/**
	 * 卡手工解除锁定
	 * 
	 * @param webInput
	 * @param cardNo
	 * @throws PCPSApplicationException
	 */
	@Override
	public String cardManualLockFree(PCPSWebInput webInput, String cardNo,
			String remark) throws PCPSApplicationException {
		// 登录用户检测
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"登录操作员所属销售机构号为空!");
		}
		// 卡号为空
		if (PCPSUtil.isEmpty(cardNo)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"检索失败,卡号不能为空!");
		}
		// 卡是否存在
		ActivateCard ac = acDao.getActivateCardByCardNoForUpdate(cardNo);
		if (null == ac) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"检索失败,没有找到此卡信息!");
		}
		// 卡所属发卡商和操作员所属发卡商是否一致
		MakeCardControl mcc = mccDao.getMakeCardControlByBatchNo(ac
				.getBatchNo());
		if (mcc != null) {
			if (!mcc.getIssueCompanyNo().equals(
					PCPSUtil.getIssueCompanyNo(webInput))) {
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"卡所属发卡商和操作员所属发卡商不一致!");
			}
		}
		// 判断卡状态
		if (!ac.getManualLockStatus().equals(
				ActivateCard.ACTIVATECARD_MANUALLOCKSTATUS_LOCKED)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"此卡未锁定或已经解锁!");
		}
		ac
				.setManualLockStatus(ActivateCard.ACTIVATECARD_MANUALLOCKSTATUS_LOCKED_FREE);
		acDao.update(ac);

		// 卡事件日志
		CardEventLog cel = new CardEventLog();
		cel.setCardEventId(commonDaoImpl.getUniqueIntegerKey());
		cel.setCardNo(cardNo);
		cel.setCardClassicId(ac.getCardClassicId());
		cel.setExpired(ac.getExpired());
		IssueCompany ic = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(PCPSUtil
						.getIssueCompanyNo(webInput));
		cel.setIssueCompanyNo(ic.getIssueCompanyNo());
		cel.setTranDate(ic.getTranDate());
		cel.setTranTime(commonDaoImpl.getDatabaseSysTime());
		cel.setBranchNo(webInput.getOperBranchNo());
		cel.setMasterAccNo(ac.getMasterAccNo());
		if (ac.getOwnerId() != null) {
			PersonalCustom pcObj = pcDao.getPersonalCustomByPersonalId(ac
					.getOwnerId());
			if (pcObj != null) {
				cel.setCardHolder(pcObj.getName());
				cel.setCardHolderIdType(pcObj.getIdTypeId());
				cel.setCardHolderIdNo(pcObj.getIdNo());
			}
		}
		cel.setCardEventOrderSeq(createCardEventSeqNo(webInput
				.getOperBranchNo(), ic));
		cel.setEventType(CardEventLog.CARDEVENTLOG_EVENTTYPE_MANUAL_LOCK_FREE);
		cel.setBalance(ac.getBalance());
		cel.setFee(new BigInteger("0"));
		cel.setOperId(webInput.getOperId());
		cel.setRemark(remark);
		celDao.insert(cel);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Edit, "解除卡手工锁定", "CardNo:"
						+ cel.getCardNo());
		return cel.getCardEventId().toString();
	}

	/**
	 * 卡临时挂失
	 * 
	 * @param cardNo
	 * @param remark
	 */
	@Override
	public String cardTemporaryLoss(PCPSWebInput webInput, String cardNo,
			String remark) throws PCPSApplicationException {
		// 登录用户检测
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"登录操作员所属销售机构号为空!");
		}
		// 卡号为空
		if (PCPSUtil.isEmpty(cardNo)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"检索失败,卡号不能为空!");
		}

		// 卡是否存在
		ActivateCard ac = acDao.getActivateCardByCardNoForUpdate(cardNo);
		if (null == ac) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"检索失败,没有找到此卡信息!");
		}
		// 卡所属发卡商和操作员所属发卡商是否一致
		MakeCardControl mcc = mccDao.getMakeCardControlByBatchNo(ac
				.getBatchNo());
		if (mcc != null) {
			if (!mcc.getIssueCompanyNo().equals(
					PCPSUtil.getIssueCompanyNo(webInput))) {
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"卡所属发卡商和操作员所属发卡商不一致!");
			}
		}
		// 判断卡是否有实名信息
		if (ac.getOwnerId() == null) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"此卡为非实名卡,不能挂失!");
		}
		PersonalCustom pcObj = pcDao.getPersonalCustomByPersonalId(ac
				.getOwnerId());
		if (pcObj == null) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"该卡的实名制信息未获取到!");
		}
		// 判断卡状态
		if (ac.getLossStatus() != null
				&& (ac.getLossStatus().equals(
						ActivateCard.ACTIVATECARD_LOSSSTATUS_TEMP_LOSSED) || ac
						.getLossStatus().equals(
								ActivateCard.ACTIVATECARD_LOSSSTATUS_LOSSED))) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"操作失败,此卡" + cardNo + " 已挂失!");
		}
		if (!ac.getCloseStatus().equals(
				ActivateCard.ACTIVATECARD_CLOSESTATUS_NORMAL)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"操作失败,此卡" + cardNo + " 已销卡!");
		}
		if (ac.getManualLockStatus().equals(
				ActivateCard.ACTIVATECARD_MANUALLOCKSTATUS_LOCKED)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"操作失败,此卡" + cardNo + " 处于锁定状态!");
		}
		ac.setLossStatus(ActivateCard.ACTIVATECARD_LOSSSTATUS_TEMP_LOSSED);
		ac.setLastLossTime(commonDaoImpl.getDatabaseSysTime());
		acDao.update(ac);

		IssueCompany ic = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(PCPSUtil
						.getIssueCompanyNo(webInput));
		// 1.卡挂失业务日志
		CardLoss cl = new CardLoss();
		cl.setCardLossId(commonDaoImpl.getUniqueIntegerKey());
		cl.setCardLossOrderSeq(createCardEventSeqNo(webInput.getOperBranchNo(),
				ic));
		cl.setCardNo(cardNo);
		cl.setRemark(remark);
		cl.setMasterAccNo(ac.getMasterAccNo());
		cl.setIssueCompanyNo(PCPSUtil.getIssueCompanyNo(webInput));
		cl.setBranchNo(webInput.getOperBranchNo());
		cl.setCardHolder(pcObj.getName());
		cl.setCardHolderIdNo(pcObj.getIdNo());
		cl.setCardHolderIdType(pcObj.getIdTypeId());
		cl.setTranType(CardLoss.CARDLOSS_TRANTYPE_TEMPORARY);
		cl.setBalance(ac.getBalance());
		cl.setOperId(webInput.getOperId());
		cl.setTranTime(commonDaoImpl.getDatabaseSysTime());
		cl.setTranDate(ic.getTranDate());
		// 插入挂失信息
		clDao.insert(cl);

		// 2.卡事件日志
		CardEventLog cel = new CardEventLog();
		cel.setCardEventId(commonDaoImpl.getUniqueIntegerKey());
		cel.setCardNo(cardNo);
		cel.setCardClassicId(ac.getCardClassicId());
		cel.setExpired(ac.getExpired());
		cel.setIssueCompanyNo(ic.getIssueCompanyNo());
		cel.setTranDate(ic.getTranDate());
		cel.setTranTime(commonDaoImpl.getDatabaseSysTime());
		cel.setBranchNo(webInput.getOperBranchNo());
		cel.setMasterAccNo(ac.getMasterAccNo());
		if (ac.getOwnerId() != null) {
			if (pcObj != null) {
				cel.setCardHolder(pcObj.getName());
				cel.setCardHolderIdType(pcObj.getIdTypeId());
				cel.setCardHolderIdNo(pcObj.getIdNo());
			}
		}
		cel.setEventType(CardEventLog.CARDEVENTLOG_EVENTTYPE_CARD_TEMP_LOSS);
		cel.setCardEventOrderSeq(createCardEventSeqNo(webInput
				.getOperBranchNo(), ic));
		cel.setBalance(ac.getBalance());
		cel.setFee(new BigInteger("0"));
		cel.setOperId(webInput.getOperId());
		cel.setRemark("卡临时挂失,卡号:" + cl.getCardNo());
		celDao.insert(cel);

		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Edit, "卡临时挂失", "CardNo:" + cl.getCardNo());
		return cl.getCardLossId().toString();
	}

	/**
	 * 卡解挂失
	 */
	@Override
	public String cardRelieveLoss(PCPSWebInput webInput, String cardNo,
			String remark) throws PCPSApplicationException {
		// 登录用户检测
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"登录操作员所属销售机构号为空!");
		}
		// 卡号为空
		if (PCPSUtil.isEmpty(cardNo)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"检索失败,卡号不能为空!");
		}

		// 卡是否存在
		ActivateCard ac = acDao.getActivateCardByCardNoForUpdate(cardNo);
		if (null == ac) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"检索失败,没有找到此卡信息!");
		}
		// 卡所属发卡商和操作员所属发卡商是否一致
		MakeCardControl mcc = mccDao.getMakeCardControlByBatchNo(ac
				.getBatchNo());
		if (mcc != null) {
			if (!mcc.getIssueCompanyNo().equals(
					PCPSUtil.getIssueCompanyNo(webInput))) {
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"卡所属发卡商和操作员所属发卡商不一致!");
			}
		}
		// 判断卡是否有实名信息
		if (ac.getOwnerId() == null) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"此卡为非实名卡,不能挂失!");
		}
		PersonalCustom pcObj = pcDao.getPersonalCustomByPersonalId(ac
				.getOwnerId());
		if (pcObj == null) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"该卡的实名制信息未获取到!");
		}
		// 判断卡状态
		if (ac.getLossStatus() != null
				&& ac.getLossStatus().equals(
						ActivateCard.ACTIVATECARD_LOSSSTATUS_LOSSED_FREE)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"操作失败,此卡" + cardNo + " 已解挂失!");
		}
		if (ac.getLossStatus() != null
				&& ac.getLossStatus().equals(
						ActivateCard.ACTIVATECARD_LOSSSTATUS_NORMAL)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"操作失败,此卡" + cardNo + " 未挂失!");
		}
		if (!ac.getCloseStatus().equals(
				ActivateCard.ACTIVATECARD_CLOSESTATUS_NORMAL)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"操作失败,此卡" + cardNo + " 已销卡!");
		}
		if (ac.getManualLockStatus().equals(
				ActivateCard.ACTIVATECARD_MANUALLOCKSTATUS_LOCKED)) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"操作失败,此卡" + cardNo + " 处于锁定状态!");
		}
		ac.setLossStatus(ActivateCard.ACTIVATECARD_LOSSSTATUS_LOSSED_FREE);
		ac.setLastLossTime(commonDaoImpl.getDatabaseSysTime());
		acDao.update(ac);
		IssueCompany ic = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(PCPSUtil
						.getIssueCompanyNo(webInput));

		// 1.卡挂失业务日志
		CardLoss cl = new CardLoss();
		cl.setCardLossId(commonDaoImpl.getUniqueIntegerKey());
		// 挂失单号
		cl.setCardLossOrderSeq(createCardEventSeqNo(webInput.getOperBranchNo(),
				ic));
		cl.setCardNo(cardNo);
		cl.setRemark(remark);
		cl.setMasterAccNo(ac.getMasterAccNo());
		cl.setIssueCompanyNo(PCPSUtil.getIssueCompanyNo(webInput));
		cl.setBranchNo(webInput.getOperBranchNo());
		cl.setCardHolder(pcObj.getName());
		cl.setCardHolderIdNo(pcObj.getIdNo());
		cl.setCardHolderIdType(pcObj.getIdTypeId());
		cl.setTranType(CardLoss.CARDLOSS_TRANTYPE_RELIEVE);
		cl.setBalance(ac.getBalance());
		cl.setOperId(webInput.getOperId());
		cl.setTranTime(commonDaoImpl.getDatabaseSysTime());
		cl.setTranDate(ic.getTranDate());
		// 插入挂失信息
		clDao.insert(cl);

		// 2.卡事件日志
		CardEventLog cel = new CardEventLog();
		cel.setCardEventId(commonDaoImpl.getUniqueIntegerKey());
		cel.setCardNo(cardNo);
		cel.setCardClassicId(ac.getCardClassicId());
		cel.setExpired(ac.getExpired());
		cel.setIssueCompanyNo(ic.getIssueCompanyNo());
		cel.setTranDate(ic.getTranDate());
		cel.setTranTime(commonDaoImpl.getDatabaseSysTime());
		cel.setBranchNo(webInput.getOperBranchNo());
		cel.setMasterAccNo(ac.getMasterAccNo());
		if (ac.getOwnerId() != null) {
			if (pcObj != null) {
				cel.setCardHolder(pcObj.getName());
				cel.setCardHolderIdType(pcObj.getIdTypeId());
				cel.setCardHolderIdNo(pcObj.getIdNo());
			}
		}
		cel.setEventType(CardEventLog.CARDEVENTLOG_EVENTTYPE_CARD_UN_LOSS);
		cel.setCardEventOrderSeq(createCardEventSeqNo(webInput
				.getOperBranchNo(), ic));
		cel.setBalance(ac.getBalance());
		cel.setFee(new BigInteger("0"));
		cel.setOperId(webInput.getOperId());
		cel.setRemark("卡解除挂失,卡号:" + cl.getCardNo());
		celDao.insert(cel);

		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Edit, "卡解挂失", "CardNo:" + cl.getCardNo());
		return cl.getCardLossId().toString();
	}

}
